package com.mc.servlet;
import com.mc.bean.*;
import com.mc.service.*;
import com.mc.service.impl.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * @author wangcf
 * @version 1.0
 * @Description 赛事添加团队控制器
 * @date 2023/2/16 13:26
 */
@WebServlet("/matchaddteam.action")
public class MatchAddTeamServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplyService applyService = new ApplyServiceImpl();
        STService stService = new STServiceImpl();
        MatchVOService matchVOService = new MatchVOServiceImpl();
        TeamService teamService = new TeamServiceImpl();
        //获取type判断是第几次进入
        String type = req.getParameter("type");
        //获取会话中的用户
        HttpSession session = req.getSession();
        Object obj = session.getAttribute("user");
        //获取要给赛事添加队伍的赛事id
        int mid = Integer.parseInt(req.getParameter("mid"));
        //指示灯，表示用户没有可以参赛
        boolean flag = false;
        Student student = (Student) obj;
        List<ST> stList = stService.findBySid(student.getSid());
        //创建个集合存放这个学生的所有队伍对象
        List<Team> teamList = new ArrayList<>();
        for (ST st : stList) {
            Team team = teamService.findTeamId(st.getTeamid());
            teamList.add(team);
        }
        //根据赛事编号查到这个赛事的所有参赛学生
        List<Student> studentList = matchVOService.findByMidInfoStu(mid);
        for (Student student1 : studentList) {
            //判断用户是否报名过了
            if (student.getSid() == student1.getSid()) {
                flag = true;
            }
        }
        //判断第几次进入
        if (type.equals("1")) {
            req.setAttribute("mid", mid);
            //把这个团队列表传到前端用来显示这个用户的团队列表
            req.setAttribute("teamlist", teamList);
            req.getRequestDispatcher("matchaddteam.jsp").forward(req, resp);
        } else if (type.equals("2")) {
            //true已报名，false未报名
            if (flag) {
                req.setAttribute("mess", "已报名，不能重复报名");
                req.setAttribute("mid", mid);
                //把这个团队列表传到前端用来显示这个用户的团队列表
                req.setAttribute("teamlist", teamList);
                //未添加成功提示用户
                req.getRequestDispatcher("matchaddteam.jsp").forward(req, resp);
            } else {
                int teamid = Integer.parseInt(req.getParameter("teamid"));
                //调用业务逻辑给这个赛事添加一个团队
                boolean i = applyService.add(new Apply(mid, teamid));
                System.out.println(i);
                //添加成功后刷新页面
                resp.sendRedirect("apply.action?mid=" + mid);
            }

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
